// site/coupon/coupon.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import wechat from '../../utils/wechat.js';
import notice from '../../utils/notice.js';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        site: {},
        coupon: {},
        count: 1,
        amount: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let site = JSON.parse(decodeURIComponent(options.site));
        let coupon = JSON.parse(decodeURIComponent(options.coupon))
        this.setData({
            site,
            coupon,
            amount: coupon.amount
        })
    },

    plusTap() {
        let ablePayNumbers = this.data.count;
        let count = this.data.count + 1;
        if (ablePayNumbers != -1) {
            if (count < ablePayNumbers) {
                count = ablePayNumbers;
                wx.showToast({
                    icon: 'none',
                    title: '最多可购买' + ablePayNumbers + '件'
                })
            }
        }
        this.setData({
            count,
            amount: this.data.coupon.amount * count
        })
    },

    minusTap() {
        let count = this.data.count - 1;
        if (count < 1) {
            count = 1;
            wx.showToast({
                icon: 'none',
                title: '购买数量不能小于1'
            })
        }

        this.setData({
            count,
            amount: this.data.coupon.amount * count
        })
    },

    publishTap() {
        let userInfo = app.globalData.userInfo;
        network.postJson(api.couponOrderCreate, {
            publisherId: userInfo.id,
            // ableUseNumbers: this.data.count,
            orderNum: this.data.count,
            amount: this.data.amount,
            siteId: this.data.site.id,
            couponType: this.data.coupon.couponType
        }).then(res => {
            let orderNo = res.data;
            network.postJson(api.prepay, {
                description: this.data.coupon.couponName,
                outTradeNo: res.data,
                amount: this.data.amount,
                openid: userInfo.openId,
                orderType: 3 // 1=活动，2=场地，3=卡券
            }).then(data => {
                let result = data.data;
                wx.requestPayment({
                    timeStamp: result.timeStamp,
                    nonceStr: result.nonceStr,
                    package: result.packageVal,
                    signType: result.signType,
                    paySign: result.paySign,
                    success: res => {
                        notice.postNotice("ActivePublish", {
                            siteId: this.data.site.id,
                            name: this.data.site.name,
                            accommodate: this.data.site.accommodate,
                            couponType: this.data.coupon.couponType
                        })
                        wechat.navigateBackPage("active/publish/publish");
                        // wx.reLaunch({
                        //     url: '/active/publish/publish?siteId=' + this.data.site.id + '&name=' + this.data.site.name + '&accommodate=' + this.data.site.accommodate + '&couponId=' + this.data.coupon.id
                        // })
                    },
                    fail(res) {
                        wx.navigateTo({
                            url: '/myself/order/order-details?orderNo=' + orderNo
                        })
                    }
                })
            })
        })
    }
})